//
//  NDLoadMoreContentView.m
//  JiMeiUnv
//
//  Created by devp on 14-9-22.
//  Copyright (c) 2014年 nd. All rights reserved.
//

#import "NDRefreshLoadMoreContainView.h"

#import "FBKVOController.h"

@interface NDRefreshLoadMoreContentView ()
@property (nonatomic, strong) UILabel                 *messageLabel;
@property (nonatomic, strong) UIActivityIndicatorView *activityView;
@property (nonatomic, strong) FBKVOController         *kvoController;
@end

@implementation NDRefreshLoadMoreContentView

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    self.backgroundColor = [UIColor clearColor];
    
    _activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    _activityView.hidden = YES;
    [self addSubview:_activityView];
}

- (void)dealloc {
}

- (BOOL)nd_refreshControlCanTriggered:(NDRefreshControl *)refreshControl {
    return self.loadMoreFlag == NDRefreshLoadMoreNormal;
}

- (void)nd_refreshControlWillTriggerRefresh:(NDRefreshControl *)refreshControl {
    
    [self showActivityIndicatorView:YES];
}

- (void)nd_refreshControlWillFinishRefresh:(NDRefreshControl *)refreshControl {
    
    [self showActivityIndicatorView:NO];
}

- (void)nd_refreshControl:(NDRefreshControl *)refreshControl scrollViewDraggedOffset:(CGFloat)offset progress:(CGFloat)progress {
    if (_loadMoreFlag == NDRefreshLoadMoreNormal) {
        if (progress < 1.0f) {
            self.messageLabel.text = @"上拉加载更多";
        } else {
            self.messageLabel.text = @"松开刷新";
        }
    } else if (_loadMoreFlag == NDRefreshLoadMoreNoMore) {
        self.messageLabel.text = @"没有更多了~";
    }
}

#pragma mark - getter
- (UILabel *)messageLabel {
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _messageLabel.font = [UIFont systemFontOfSize:14];
        _messageLabel.backgroundColor = [UIColor clearColor];
        _messageLabel.textColor = [UIColor grayColor];
        
        _kvoController = [FBKVOController controllerWithObserver:self];
        __weak UILabel *wLabel = _messageLabel;
        [_kvoController observe:_messageLabel keyPath:@"text" options:NSKeyValueObservingOptionNew block:^(id observer, id object, NSDictionary *change) {
            UILabel *sLabel = wLabel;
            [sLabel sizeToFit];
            UIView *superView = sLabel.superview;
            if (superView) {
                sLabel.center = superView.center;
            }
        }];
        [self addSubview:_messageLabel];
    }
    
    return _messageLabel;
}

#pragma mark - private
- (void)showActivityIndicatorView:(BOOL)showFlag {
    if (showFlag) {
        if (!_activityView.isAnimating) {
            [_activityView startAnimating];
        }
        _messageLabel.hidden = YES;
        _activityView.hidden = NO;
    } else {
        if (_activityView.isAnimating) {
            [_activityView stopAnimating];
        }
        _messageLabel.hidden = NO;
        _activityView.hidden = YES;
    }
    _activityView.center = self.center;
    
    [self setNeedsDisplay];
}

@end
